package org.mch.start;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.apache.log4j.Logger;
import org.mch.comm.itf.IMLogger;

import com.mch.main.common.MDebugDLG;

public class MUILogger implements IMLogger {

	private Logger log;

	public MUILogger(Class<?> clazz) {
		super();
		log = Logger.getLogger(clazz);
	}

	@Override
	public void debug(Object obj) {
		log.debug(obj);
		common(obj, null);
	}

	@Override
	public void debug(Object obj, Throwable e) {
		log.debug(obj, e);
		common(obj, e);
	}

	@Override
	public void debug(Throwable e) {
		log.debug(e.getMessage(), e);
		common(null, e);
	}

	@Override
	public void info(Object obj) {
		log.info(obj);
		common(obj, null);
	}

	@Override
	public void info(Object obj, Throwable e) {
		log.info(obj, e);
		common(obj, e);
	}

	@Override
	public void info(Throwable e) {
		log.info(e.getMessage(), e);
		common(null, e);
	}

	@Override
	public void error(Object obj) {
		log.error(obj);
		common(obj, null);
	}

	@Override
	public void error(Throwable e) {
		log.error(e.getMessage(), e);
		common(null, e);
	}

	@Override
	public void error(Object obj, Throwable e) {
		log.error(obj, e);
		common(obj, e);
	}

	private void common(Object obj, Throwable e) {
		if (obj != null) {
			MDebugDLG.debug(obj);
		}
		if (e != null) {
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			PrintStream ps = new PrintStream(os);
			e.printStackTrace(ps);

			MDebugDLG.debug(os.toString());
		}
	}

	@Override
	public void showMsg(String msg) {
	}

	@Override
	public void showDebug(String msg) {
	}
}
